<?php
/**
 * Created by PhpStorm.
 * User: 38339
 * Date: 2021-05-26
 * Time: 14:58
 */

namespace app\common\business\auth;
use app\common\business\sys\CommonSysIpConfig;
use app\common\business\sys\CommonSysOtherConfig;
use app\common\model\auth\CommonAdmin as Model;


class CommonAdmin
{

    /**
     * 【方法注释】检查token
     * @param $token 用户唯一标识
     * @return array|null|\think\Model 返回admin对象
     */
    public static function checkToken($token){
        $admin = Model::getOne(['token'=>$token]);
        if(empty($admin)){
            jsonFail('请重新登录',7);
        }
        if($admin['token_expire_at'] <= date('Y-m-d H:i:s')){
            jsonFail('token过期，请重新登录');
        }
        if($admin['status'] != 1){
            jsonFail('该帐号已禁用',7);
        }
        return $admin;
    }

    /**
     * 【方法注释】登录
     * @param $load 传入对象
     * @return string  返回生成的token
     */
    public static function login($load){
        $token = api_token($load['id']);
        Model::where(['id'=>$load['id']])->update(
            [
                'token'=>$token,
                'token_expire_at'=>date('Y-m-d H:i:s',strtotime('+12 hour')),
                'update_time' => date('Y-m-d H:i:s')
            ]);
        return $token;
    }


    public static function checkLoginIp(){
        $login_ip = get_server_ip();
        $otherConfig = CommonSysOtherConfig::getOne();
        if(isset($otherConfig['start_ip_status'])){
            switch ($otherConfig['start_ip_status']){
                case 2:
                    $load = CommonSysIpConfig::getOne(['ip'=>$login_ip,'status'=>0]);
                    if(!empty($load)){
                        jsonFail('您当前的IP不能登录当前后台，请联系管理员');
                    }
                    break;
                case 3:
                    $load = CommonSysIpConfig::getOne(['ip'=>$login_ip,'status'=>1]);
                    if(empty($load)){
                        jsonFail('您当前的IP不能登录当前后台，请联系管理员');
                    }
                    break;
                default:
                    break;
            }
            //如果该字段已经被删掉，则只校验禁止
        }
        return true;
    }

    /**
     * 【方法注释】获取单条数据
     * @param $where 查询条件数组
     * @return array|null|\think\Model 返回单个对象
     */
    public static function getOne($where){
        return Model::getOne($where);
    }

    /**
     * 【方法注释】获取数据
     * @param $where 查询条件数组
     * @param $limit_start 第几页
     * @param $page_size 取多少条
     * @return array 返回数据列表
     */
    public static function getList($where,$limit_start,$page_size){
        return Model::getList($where,$limit_start,$page_size);
    }

    /**
     * 【方法注释】获取数据条数
     * @param $where 查询条件数组
     * @return int 返回符合条件的数据条数
     */
    public static function getCount($where){
        return getAllPage(Model::getCount($where));
    }

    /**
     * 【方法注释】新增数据
     * @param $receiveData 数据数组
     * @return bool 返回是否成功
     */
    public static function insert($receiveData){
        $data = [
            'username' => $receiveData['username'],
            'city_config_arr' => json_encode($receiveData['city_config_arr']),
            'status' => $receiveData['status'] == 1 ? 1 : 0,
            'role_id' => $receiveData['role_id'],
            'city_config_id' => end($receiveData['city_config_arr']),
            'remark' => $receiveData['remark'],
            'mobile' => $receiveData['mobile'],
            'password' => pwdMd5($receiveData['password']),
            'create_time' => date('Y-m-d H:i:s'),
            'update_time' => date('Y-m-d H:i:s')
        ];
        Model::insert($data);
        return true;
    }

    /**
     * 【方法注释】修改数据
     * @param $receiveData 数据数组
     * @return bool 返回是否成功
     */
    public static function edit($receiveData){
        $data = [
            'username' => $receiveData['username'],
            'city_config_arr' => json_encode($receiveData['city_config_arr']),
            'status' => $receiveData['status'] == 1 ? 1 : 0,
            'role_id' => $receiveData['role_id'],
            'city_config_id' => end($receiveData['city_config_arr']),
            'remark' => $receiveData['remark'],
            'mobile' => $receiveData['mobile'],
            'update_time' => date('Y-m-d H:i:s')
        ];
        if(!empty($receiveData['password'])){
            $data['password'] = pwdMd5($receiveData['password']);
        }
        Model::where(['id'=>$receiveData['id']])->update($data);
        return true;
    }

    /**
     * 【方法注释】更新数据库字段
     * @param $value 结果值
     * @param $load 源数据对象
     * @return bool
     */
    public static function updateStatus($value,$load){
        Model::where(['id'=>$load['id']])->update([
            'status' => $value,
            'update_time' => date('Y-m-d H:i:s')
        ]);
        return true;
    }

}